/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package menu;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
/**
 *
 * @author maurizio
 */
public class XMLprocessor {

    private TemplateManager tpl;
    private HashMap data;
    private OutputStreamWriter out;
    private JTree tree;



    public XMLprocessor(JTree selectiontree) {
            tpl = new TemplateManager();
            data = (HashMap) tpl.getDefaultDataModel();
            tree=selectiontree;
            //tpl.setOutlineTemplate("content");
            //tpl.setDefaultData("portate",readtree(tree));
        }    

    private LinkedHashMap readtree (JTree tree){
        Map portate = new LinkedHashMap<String,HashMap>();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
        int NPort = tree.getModel().getChildCount(root),NPiatti=0, i=0,j=0;
        String portata;
        String piatto;
        if (NPort != 0) {           
            for (i=0;i<NPort; i++){
                portata=((root.getChildAt(i).toString()).substring(5));
                NPiatti=tree.getModel().getChildCount(root.getChildAt(i));//numero di piatti associato alla portata
                Map piatti = new LinkedHashMap<String,String>();
                for (j=0; j<NPiatti; j++){
                    piatto=root.getChildAt(i).getChildAt(j).toString();
                    if (!(root.getChildAt(i).getChildAt(j).isLeaf())){
                        piatti.put(piatto, root.getChildAt(i).getChildAt(j).getChildAt(0).toString());
                    }else{
                        piatti.put(piatto,null);
                    }
                }
                portate.put(portata,piatti);
            }
        }
        return (LinkedHashMap) portate;
    }

    private String findpath(String os){
        String dir = new String();
        os=os.toLowerCase();
        if (os.startsWith("linu")){
            dir ="odttemp/";
        }else{
            dir=(System.getProperty("user.dir"))+"\\odttemp\\";
        }
        return dir;
    }

    void process () throws UnsupportedEncodingException{
            data.put("portate", readtree(tree));
        try {
            //System.out.println(findpath(System.getProperty("os.name").toLowerCase()));
            OutputStream output = new FileOutputStream(findpath(System.getProperty("os.name")) + "content.xml");
            out= new OutputStreamWriter(output,"UTF8");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(XMLprocessor.class.getName()).log(Level.SEVERE, null, ex);
        }
            tpl.process("content", data, out);
            
    }


}
